package list;

// 2、无头双向链表实现
public class LinkedList {
    private class LinkNode{
        public int val;
        LinkNode prev;
        LinkNode next;

        public LinkNode(int val) {
            this.val = val;
        }
    }
    private LinkNode head;
    private LinkNode tail;
    //头插法
    public void addFirst(int data){
        LinkNode node = new LinkNode(data);
        if(head == null){
            head = node;
            tail = node;
            return;
        }
        node.next = head;
        head.prev = node;
        head = node;
    };
    //尾插法
    public void addLast(int data){
        LinkNode node = new LinkNode(data);
        tail.next = node;
        node.prev = tail;
        tail = node;
    };
    //任意位置插入,第一个数据节点为0号下标
    public boolean addIndex(int index,int data){

        if(index==0){
            addFirst(data);
            return true;
        }
        if(index>=size()){
            addLast(data);
            return true;
        }
        LinkNode node = new LinkNode(data);
        LinkNode cur = head;
        while (index>0){
            index--;
            cur = cur.next;
        }
        node.prev = cur.prev;
        node.next = cur.next;
        node.prev.next = node;
        node.next.prev = node;
        return true;
    };
    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        LinkNode cur = head;
        while (cur!=null){
            if(cur.val==key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    };
    //删除第一次出现关键字为key的节点
    public void remove(int key){
        if(head == null){
            return;
        }
        LinkNode cur = head;
        while (cur!=null){
            if(cur.val == key){
                break;
            }
            cur = cur.next;
        }
        if(cur == head){
            head = cur.next;
            cur.prev = null;
            return;
        }
        if(cur == tail){
            tail = cur.prev;
            tail.next = null;
            return;
        }
        cur.prev.next = cur.next;
        cur.next.prev = cur.prev;
    };
    //删除所有值为key的节点
    public void removeAllKey(int key){
        if(head == null){
            return;
        }
        LinkNode cur = head;
        while (cur!=null){
            if(cur.val == key){
                if(cur == head){
                    head = cur.next;
                    cur.prev = null;
                    cur = cur.next;
                    continue;
                }
                if(cur == tail){
                    tail = cur.prev;
                    tail.next = null;
                    cur = cur.next;
                    continue;}
                cur.prev.next = cur.next;
                cur.next.prev = cur.prev;
            }
            cur = cur.next;
        }
    };
    //得到单链表的长度
    public int size(){
        int length = 0;
        LinkNode cur = head;

        while (cur!=null){
            length++;
            cur = cur.next;
        }
        return length;
    };
    public void display(){
        LinkNode cur = head;

        while (cur!=null){
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
        System.out.println();
    };
    public void clear(){
        if(head == null){
            return;
        }
        LinkNode cur = head;
        LinkNode curN = head.next;
        while (cur!=null){
              curN = cur.next;
              cur.next = null;
              cur.prev = null;
              cur.val = 0;
              cur = curN;
        }
        head = null;
        tail = null;
    };
}